<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use Hash;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DetailController extends Controller
{
   public function getIndex()
   {
    // return 'qddfsdf';
    // $s = Hash::make('1234567890');
    // dd($s);
    $id = 1;
    $book = DB::table('hs_books')
        ->join('hs_member','hs_member.id','=','hs_books.aid')
        ->where('hs_books.id',$id)->first();
    // dd($book);
    $comments = DB::table('hs_comment')
        ->join('hs_articles','hs_articles.id','=','hs_comment.artid')
        ->join('hs_member','hs_member.id','=','hs_comment.publish_id')
        ->join('hs_books','hs_books.id','=','hs_articles.bid')
        // ->join('hs_ext','hs_ext.aid','=','hs_books.aid')
        ->where(['hs_books.id'=>$id,'hs_comment.reply_id'=>0])
        ->select('hs_comment.com_content','hs_member.name','hs_comment.com_time','hs_articles.id','hs_comment.publish_id','hs_comment.artid')
        ->get(); 
    //次级回复
    $i = 0;    
    foreach($comments as $k=>$v){
        
        $comm[$i]['name'] = $v->name;
        $comm[$i]['com_content'] = $v->com_content;
        $comm[$i]['com_time'] = $v->com_time;
        $comm[$i]['id'] = $v->id;
        $comm[$i]['publish_id'] = $v->publish_id;
        $comm[$i]['artid'] = $v->artid;
        // var_dump($v->name);
        // var_dump($v->com_content);
        // var_dump($v->com_time);
        // var_dump($v->id);
        // var_dump($v->publish_id);
        $ccomments = DB::table('hs_comment')
        ->join('hs_articles','hs_articles.id','=','hs_comment.artid')
        ->join('hs_member','hs_member.id','=','hs_comment.publish_id')
        ->join('hs_books','hs_books.id','=','hs_articles.bid')
        // ->join('hs_ext','hs_ext.aid','=','hs_books.aid')
        ->where(['hs_books.id'=>$id,'hs_comment.reply_id'=>$v->publish_id])
        ->select('hs_comment.com_content','hs_member.name','hs_comment.com_time','hs_articles.id','hs_comment.publish_id','hs_comment.artid')
        ->get();
        $comm[$i]['ccomm'] = $ccomments;
        // var_dump($ccomments); 
        $i++;
    }
     // var_dump($comm);
    // die;

    // dd($comments);  

    $article = DB::table('hs_articles')->where('bid','=',$id)->orderBy('time', 'desc')->first();
    // dd($article);  

    //查询作者发布公告和推荐
    $author = DB::table('hs_ext')
        ->join('hs_books','hs_books.id','=','hs_ext.aid')
        ->join('hs_member','hs_member.id','=','hs_books.aid')
        ->where('hs_books.id',$id)
        ->select('hs_ext.gg','hs_ext.tj','hs_member.name','hs_member.pic')
        ->first();
    if(!$author->pic){
        $author->pic = '/public/home/images/detail/noavatar_small.gif';
    }    

     $au = DB::table('hs_books')->where('hs_books.id','=',$id)->select('hs_books.aid')->first();
     //获取作者id
         $auid = $au->aid;
     //查询作者所有的书籍
         $books = DB::table('hs_books')->where('hs_books.aid','=',$auid)->select('hs_books.b_name')->get();
        // dd($books);   

    return view('home.details.index',['book'=>$book,'comments'=>$comm,'article'=>$article,'author'=>$author,'books'=>$books]);
   }

   public function postInsert(Request $request)
   {
        $data = $request->except('_token');
        $data['publish_id'] = $request->input('publish_id');
        $data['reply_id'] = 0;
        $data['com_time'] = time();
        $data['status'] = 0;
        $res = DB::table('hs_comment')->insertGetId($data);
        if($res){
            return redirect('/home/detail/index');
        }
        // return 'asdfsd';
   }
   //详情页回复方法
   public function postRinsert(Request $request)
   {
        // dd($request->all());
        $data = $request->except('_token');
        $data['status'] = 0;
        $data['com_time'] = time();
        // dd($data);
        $res = DB::table('hs_comment')->insert($data);
        if($res){
            return back()->with('success','回复成功');
        }else{
            return back()->with('error','回复失败');
        }
   }
}